﻿import mx.events.EventDispatcher;
import mx.utils.Delegate;

import idv.ben.picasawebservice4flash.data.*;
import idv.ben.picasawebservice4flash.service.*;

class idv.ben.picasawebservice4flash.service.PicasawebService{
	public static var SERVICE_URL:String = "http://picasaweb.google.com/data/feed/api/";
	
	public static var EVENT_FEED_ONLOAD:String = "EVENT_FEED_ONLOAD";
	public static var EVENT_FEED_ONERROR:String = "EVENT_FEED_ONERROR";
	
	public static var EVENT_ENTRY_ONLOAD:String = "EVENT_ENTRY_ONLOAD";
	public static var EVENT_ENTRY_ONERROR:String = "EVENT_ENTRY_ONERROR";
		
	public var dispatchEvent:Function;
	public var addEventListener:Function;
	public var removeEventListener:Function;
	
	private var __xmlFeed:XML;
	private var __xmlEntry:XML;
	
	/**
	* 建構子
	*/
	function PicasawebService(){
		EventDispatcher.initialize(this);
	}
	
	/**
	* 開始載入 feed
	* @param userId Picasaweb 使用者帳號
	*/
	public function loadFeed(userId:String):Void{
		requestFeed(userId);
	}
	
	private function requestFeed(userId:String):Void{
		__xmlFeed = new XML();
		__xmlFeed.ignoreWhite = true;
		__xmlFeed.onLoad = Delegate.create(this, requestFeed_onLoad);
		__xmlFeed.load(SERVICE_URL + "user/" + userId + "?kind=album");
	}
	
	private function requestFeed_onLoad(success:Boolean):Void{
		if(success){
			var feed:UserFeed = parseFeed(this.__xmlFeed);
			__xmlFeed = null;

			dispatchEvent({type:EVENT_FEED_ONLOAD, target:this, feed:feed});
		}else{
			dispatchEvent({type:EVENT_FEED_ONERROR, target:this});
		}
	}
	
	private function parseFeed(xml:XML):UserFeed{
		var feed:UserFeed = new UserFeed();
		feed.gphoto = new UserGPhoto();
		feed.entrys = new Array();
		
		for(var i:Number=0; i<xml.firstChild.childNodes.length; i++){
			if(xml.firstChild.childNodes[i].nodeName.indexOf("gphoto:") == 0){
				feed.gphoto.applyParam(xml.firstChild.childNodes[i].nodeName.split("gphoto:").join(""), xml.firstChild.childNodes[i].firstChild);
				continue;
			}					
			switch(xml.firstChild.childNodes[i].nodeName.toLowerCase()){
				case "id".toLowerCase():
				case "updated".toLowerCase():
				case "title".toLowerCase():
				case "subtitle".toLowerCase():
				case "icon".toLowerCase():
					feed[xml.firstChild.childNodes[i].nodeName] = xml.firstChild.childNodes[i].firstChild;
					break;
				case "author".toLowerCase():
					feed.author_name = xml.firstChild.childNodes[i].childNodes[0].firstChild;
					feed.author_uri = xml.firstChild.childNodes[i].childNodes[1].firstChild;
					break;
				case "entry".toLowerCase():
					feed.entrys.push(new AlbumEntry(xml.firstChild.childNodes[i]));
					break;
			}
		}
		
		return feed;
	}
	
	/**
	* 開始載入 entry
	* @param entry 欲載入的相簿
	*/
	public function loadEntry(entry:AlbumEntry):Void{
		requestEntry(entry);
	}
	
	private function requestEntry(entry:AlbumEntry):Void{
		__xmlEntry = new XML();
		__xmlEntry.ignoreWhite = true;
		__xmlEntry.onLoad = Delegate.create(this, requestEntry_onLoad);
		__xmlEntry.load(SERVICE_URL + "user/" + entry.gphoto.user + "/albumid/" + entry.gphoto.id + "?kind=photo");
	}
	
	private function requestEntry_onLoad(success:Boolean):Void{
		if(success){
			var feed:AlbumFeed = parseEntry(this.__xmlEntry);
			__xmlEntry = null;

			dispatchEvent({type:EVENT_ENTRY_ONLOAD, target:this, feed:feed});
		}else{
			dispatchEvent({type:EVENT_ENTRY_ONERROR, target:this});
		}
	}
	
	private function parseEntry(xml:XML):AlbumFeed{
		var feed:AlbumFeed = new AlbumFeed();
		feed.gphoto = new AlbumGPhoto();
		feed.entrys = new Array();
		
		for(var i:Number=0; i<xml.firstChild.childNodes.length; i++){
			if(xml.firstChild.childNodes[i].nodeName.indexOf("gphoto:") == 0){
				feed.gphoto.applyParam(xml.firstChild.childNodes[i].nodeName.split("gphoto:").join(""), xml.firstChild.childNodes[i].firstChild);
				continue;
			}					
			switch(xml.firstChild.childNodes[i].nodeName.toLowerCase()){
				case "id".toLowerCase():
				case "updated".toLowerCase():
				case "title".toLowerCase():
				case "subtitle".toLowerCase():
				case "rights".toLowerCase():
				case "icon".toLowerCase():
					feed[xml.firstChild.childNodes[i].nodeName] = xml.firstChild.childNodes[i].firstChild;
					break;
				case "author".toLowerCase():
					feed.author_name = xml.firstChild.childNodes[i].childNodes[0].firstChild;
					feed.author_uri = xml.firstChild.childNodes[i].childNodes[1].firstChild;
					break;
				case "entry".toLowerCase():
					feed.entrys.push(new PhotoEntry(xml.firstChild.childNodes[i]));
					break;
			}
		}
		
		return feed;
	}
}